//
//  UserAccountCell.m
//  CoolTennisBall
//
//  Created by Coollang on 16/9/5.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

#import "UserAccountCell.h"
#import "PNChart.h"
#import "UserDataModel.h"


@interface UserAccountCell ()<PNChartDelegate>
@property (weak, nonatomic) IBOutlet UIView *yearView;
@property (weak, nonatomic) IBOutlet UIButton *previousButton;
@property (weak, nonatomic) IBOutlet UIButton *nextButton;
@property (weak, nonatomic) IBOutlet UILabel *yearNumLabel;

@property (weak, nonatomic) IBOutlet UILabel *hitBallNumLabel; // 总击球数
@property (weak, nonatomic) IBOutlet UILabel *activeTimeLabel; // 活跃时间
@property (weak, nonatomic) IBOutlet UIView *barChartSuperView;

@property (nonatomic, strong) PNBarChart *barChartView;

/** 当前显示的数据的索引 */
@property (nonatomic, assign)NSInteger currentindex;

@property (weak, nonatomic) IBOutlet UILabel *activeTimeLab;

@property (weak, nonatomic) IBOutlet UILabel *hitBallNumLab;

@end

@implementation UserAccountCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.yearView.layer.cornerRadius = 15;
    self.yearView.layer.borderWidth = 1;
    self.yearView.layer.borderColor = [UIColor colorWithHex:0xd5d5d5].CGColor;
    self.yearView.layer.masksToBounds = YES;
    [self barChartView];
    
    if ([NSBundle currentLanguageIsChinese]) {
        self.activeTimeLab.font = [UIFont systemFontOfSize:10];
        self.hitBallNumLab.font = [UIFont systemFontOfSize:10];
        self.activeTimeLab.text = @"活跃时间(h)";
        self.hitBallNumLab.text = @"击球数(次)";
    }else {
        self.activeTimeLab.font = [UIFont systemFontOfSize:12];
        self.hitBallNumLab.font = [UIFont systemFontOfSize:12];
        self.activeTimeLab.text =[NSString stringWithFormat:@"%@(h)",NSLocalizedString(@"User.Active Time", @"活跃时间")];
        self.hitBallNumLab.text = NSLocalizedString(@"Shots", nil);
    }
}

- (IBAction)previousButtonClick:(UIButton *)sender {
  
    if (self.currentindex > 0 && self.currentindex <= self.dataModel.yearMonthList.count - 1) {
        self.currentindex--;
         UserMonthDataModel *monthModel = self.dataModel.yearMonthList[self.currentindex];
        [self UpdateBarChartViewWith:monthModel];
    }
}

- (IBAction)nextButtonClick:(UIButton *)sender {
    if (self.currentindex < self.dataModel.yearMonthList.count - 1 && self.currentindex >= 0) {
        self.currentindex++;
        UserMonthDataModel *monthModel = self.dataModel.yearMonthList[self.currentindex];
        [self UpdateBarChartViewWith:monthModel];
    }
}

- (void)setDataModel:(UserDataModel *)dataModel {
    _dataModel = dataModel;
    
    self.hitBallNumLabel.text = [self checkFullString:dataModel.TotalCount];
    self.activeTimeLabel.text = [self checkFullString:dataModel.TotalDuration];
    UserMonthDataModel *monthModel = dataModel.yearMonthList.lastObject;
    self.currentindex = dataModel.yearMonthList.count - 1;
    
    
    [self UpdateBarChartViewWith:monthModel];
}
- (NSString *)checkFullString:(NSString *)string {
    
    if (string.length <= 0) {
        return @"0";
    }
    return string;
}
//更新柱状图
- (void)UpdateBarChartViewWith:(UserMonthDataModel *)monthModel {
    NSString *yearStr = monthModel.year;
    if (monthModel.year.length == 0) {
        NSString *yearMonthString = [[NSDate date] formateYearAndMonth];
        NSInteger lastYear = [[yearMonthString substringToIndex:4] integerValue];
        yearStr = @(lastYear).stringValue;
    }
    if ([NSBundle currentLanguageIsChinese]) {
        self.yearNumLabel.text = [NSString stringWithFormat:@"%@年击球数",yearStr];
    }else {
        self.yearNumLabel.text = [NSString stringWithFormat:@"%@-%@",NSLocalizedString(@"Shots", nil),yearStr];
    }
    
    self.barChartView.yMaxValue = [monthModel maxYValue];
    [self.barChartView setYValues:monthModel.yearMonthDatas];
    self.barChartView.dashLineLabels = [monthModel dashLineTargetValue];
    [self.barChartView strokeChart];
}

- (void)setCurrentindex:(NSInteger)currentindex {
    _currentindex = currentindex;
    // 设置按钮的enable属性
    if (currentindex == 0) {
        self.previousButton.enabled = NO;
    }else {
        self.previousButton.enabled = YES;
    }
    
    if (currentindex == self.dataModel.yearMonthList.count - 1) {
        self.nextButton.enabled = NO;
    }else {
        self.nextButton.enabled = YES;
    }
    
}


- (PNBarChart *)barChartView {
    if (_barChartView == nil) {

        _barChartView = [[PNBarChart alloc] initWithFrame:CGRectMake(16, 0, kYFScreenWidth - 32, 200)];
        _barChartView.backgroundColor = [UIColor clearColor];

        _barChartView.yChartLabelWidth = 20.0;
        _barChartView.chartMarginLeft = 34.0;
        _barChartView.chartMarginRight = 0.0;
        _barChartView.chartMarginTop = 25;
        _barChartView.chartMarginBottom = 15.0;
        _barChartView.showChartBorder = YES;
        [_barChartView setXLabels:@[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12"]];
        _barChartView.yMaxValue = 1000.0;
         [_barChartView setYValues:@[@0,@0,@0,@0,@0,@0,@0,@0,@0,@0,@0,@0,]];
        _barChartView.strokeColor = PNGreen;
        _barChartView.barBackgroundColor = [UIColor clearColor];
        _barChartView.barRadius = 0;
        _barChartView.showLabel = NO;
        _barChartView.isShowNumbers = NO;
        _barChartView.isGradientShow = YES;
        _barChartView.gradientColors = @[[UIColor colorWithHex:0xffc209], [UIColor colorWithHex:0xff7349]];
        _barChartView.gradientStartPoint = CGPointMake(0.5, 0);
        _barChartView.gradientEndPoint = CGPointMake(0.5, 1);
        _barChartView.isShowYLabels = NO;
        _barChartView.isShowYAxis = NO;
        _barChartView.isShowXAxis = NO;
        _barChartView.dashLines = @[@0, @0.2, @0.4, @0.6,@0.8,@1.0];
        _barChartView.dashLineLabels = @[@"0",@"200",@"400",@"600",@"800",@"1000"];
        UIColor *dashLineColor = kLineBackgroundColor;        _barChartView.dashLineColor = @[dashLineColor,dashLineColor,dashLineColor,dashLineColor,dashLineColor,dashLineColor];
        UIColor *dashTextColor = [UIColor colorWithHex:0x333333 alpha:0.5];
        _barChartView.dashLineTextColor =  @[dashTextColor,dashTextColor,dashTextColor,dashTextColor,dashTextColor,dashTextColor];
        [_barChartView strokeChart];
        _barChartView.delegate = self;
        [self.barChartSuperView addSubview:_barChartView];
    }
    
    return _barChartView;
}

- (void)userClickedOnBarAtIndex:(NSInteger)barIndex
{
    PNBar * bar = [self.barChartView.bars objectAtIndex:barIndex];
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    
    animation.fromValue = @1.0;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.toValue = @1.1;
    animation.duration = 0.2;
    animation.repeatCount = 0;
    animation.autoreverses = YES;
    animation.removedOnCompletion = YES;
    animation.fillMode = kCAFillModeForwards;
    
    [bar.layer addAnimation:animation forKey:@"Float"];
}

@end
